package controller.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Admin;
import pojo.User;
import service.admin.AdminService;
import service.user.UserService;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller

public class LoginController {
    //    controller层调service层
    //自动装配User
    @Autowired
    private UserService userService;
    public void setUserService(UserService userService){
        this.userService=userService;
    }
//自动装配管理员
    @Autowired
    private AdminService adminService;

    /**
     * 删除session，退出登录
     * @param adminService
     */
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }
    @RequestMapping(value = ("/login"), method = RequestMethod.GET)
    public String login(Model model, HttpSession session) {
        if (session.getAttribute("id") != null) {
            session.removeAttribute("id");
        }
        return "../../login";
    }

    @ResponseBody
    @RequestMapping(value = ("/login"), method = RequestMethod.POST)
    public Map<String, String> isLogin(@RequestBody Map<String, String> info, HttpSession session) {
        System.out.println(info);

        HashMap<String, String> Error = new HashMap<>();
        Error.put("info","请输入正确的用户名和密码！");
        //传入的角色是员工
        if (info.get("role").equals("user")) {
            User user = userService.queryUserById(info.get("id"));
            if (user!=null){
                if (user.getPassword().equals(info.get("pwd"))) {
                    if (session.getAttribute("id") != null) {
                        session.removeAttribute("id");
                    }
                    System.out.println(user);
                    session.setAttribute("id", user);
                    session.setMaxInactiveInterval(3600);//设置会话最大维持时间1小时
                    HashMap<String, String> toFrame = new HashMap<>();
                    System.out.println("Loading on AdminMain.jsp");
                    toFrame.put("redirect", "/user");
                    return toFrame;
                }
            }
        }
        //传入的角色是管理员
        if (info.get("role").equals("admin")) {
            Admin admin = adminService.queryById(info.get("id"));
            if (admin.getPwd().equals(info.get("pwd"))) {
                if (session.getAttribute("id") != null) {
                    session.removeAttribute("id");
                }
                System.out.println(admin);
                session.setAttribute("id", admin);
                session.setMaxInactiveInterval(3600);//设置会话最大维持时间1小时
                HashMap<String, String> toFrame = new HashMap<>();
                System.out.println("Loading on AdminMain.jsp");
                toFrame.put("redirect", "/admin");
                return toFrame;
            }
        }
        return Error;
    }
    //员工登录成功
    @RequestMapping("/user")
    public String UserLogin(Model model,HttpSession session){
        Object user = session.getAttribute("id");
        model.addAttribute("user",user);
        return "UserMain";
    }
    //管理员登录成功
    @RequestMapping("/admin")
    public String AdminLogin(Model model,HttpSession session){
        Object admin = session.getAttribute("id");
        model.addAttribute("user",admin);
        return "AdminMain";
    }

}



